﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Extension Methods</title>
    <script type="text/javascript" src="../common/commonFuncs.js"></script>
    <script type="text/javascript">
        function sample1() {
            var points = [
                { x: 0, y: 0 },
                { x: 1, y: 1}];

            points.dist = function () {
                var p1 = this[0];
                var p2 = this[1];
                var dx = p1.x - p2.x;
                var dy = p1.y - p2.y;
                return Math.sqrt(dx * dx + dy * dy);
            };

            printline("distance=" + points.dist().toFixed(2));

            // this extension is method is only attached to one instance
            // not attached to the class
            // so it cannot be accessed from another instance
            // !!! below codes will be ignored by the engine.
            var points2 = [
                { x: 1, y: 2 },
                { x: 3, y: 4}];
            printline("distance=" + points2.dist().toFixed(2));
        }
    </script>
</head>
<body>
    <script type="text/javascript">
        sample1();
    </script>
</body>
</html>
